package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.SeekBar;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.customview.img.StkWatermarkImgView;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.adapter.AngleAdapter;
import flc.ast.adapter.WaterColorAdapter;
import flc.ast.bean.MyColorBean;
import flc.ast.databinding.ActivityPicWaterMarkBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.RxUtil;

public class PicWaterMarkActivity extends BaseAc<ActivityPicWaterMarkBinding> implements SeekBar.OnSeekBarChangeListener {

    public static String imgPath;
    private AngleAdapter angleAdapter;
    private WaterColorAdapter colorAdapter;
    private int colorPos;

    @Override
    protected int onCreate() {
        return R.layout.activity_pic_water_mark;
    }

    @Override
    protected void initView() {
        mDataBinding.ivPicWaterMarkBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.rvAngle.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        angleAdapter = new AngleAdapter();
        mDataBinding.rvAngle.setAdapter(angleAdapter);
        angleAdapter.setOnItemClickListener(this);
        mDataBinding.rvColor.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        colorAdapter = new WaterColorAdapter();
        mDataBinding.rvColor.setAdapter(colorAdapter);
        colorAdapter.setOnItemClickListener(this);
        mDataBinding.sbAlpha.setOnSeekBarChangeListener(this);
        mDataBinding.sbSize.setOnSeekBarChangeListener(this);
        mDataBinding.etName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mDataBinding.watermarkImg.setWatermarkText(s.toString());
            }
        });
        mDataBinding.ivPicWaterMarkSave.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivPicWaterMarkSave://保存
                saveImg();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter == angleAdapter) {
            angleAdapter.setSelPosition(position);
            angleAdapter.notifyDataSetChanged();
            switch (position) {
                case 0:
                    mDataBinding.watermarkImg.setStyle(StkWatermarkImgView.Style.ZUO_XIE);
                    break;
                case 1:
                    mDataBinding.watermarkImg.setStyle(StkWatermarkImgView.Style.HORIZONTAL);
                    break;
                case 2:
                    mDataBinding.watermarkImg.setStyle(StkWatermarkImgView.Style.VERTICAL);
                    break;
                default:
                    break;
            }
        } else if (adapter == colorAdapter) {
            colorAdapter.getItem(colorPos).setSelect(false);
            colorAdapter.notifyItemChanged(colorPos);
            colorPos = position;
            colorAdapter.getItem(position).setSelect(true);
            colorAdapter.notifyItemChanged(position);
            mDataBinding.watermarkImg.setTextColor(Color.parseColor(colorAdapter.getItem(position).getColor()));
        }
    }

    @Override
    protected void initData() {
        String[] array = getResources().getStringArray(R.array.angle_classify);
        angleAdapter.setList(Arrays.asList(array));
        Glide.with(this).load(imgPath).into(mDataBinding.watermarkImg);
        //颜色数据
        List<MyColorBean> listColor = new ArrayList<>();
        listColor.add(new MyColorBean("#FFFFFF", true));
        listColor.add(new MyColorBean("#000000", false));
        listColor.add(new MyColorBean("#659CFF", false));
        listColor.add(new MyColorBean("#F72323", false));
        listColor.add(new MyColorBean("#B3B3B3", false));
        colorAdapter.setList(listColor);
        mDataBinding.watermarkImg.setTextColor(Color.parseColor(colorAdapter.getItem(0).getColor()));
    }

    //保存图片
    private void saveImg() {
        showDialog(getString(R.string.saving));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                RxUtil.create(new RxUtil.Callback<String>() {

                    @Override
                    public void doBackground(ObservableEmitter<String> emitter) {
                        Bitmap bitmap = ImageUtils.view2Bitmap(mDataBinding.watermarkImg);
                        ImageUtils.save2Album(bitmap, Bitmap.CompressFormat.PNG);
                        emitter.onNext("");
                    }

                    @Override
                    public void accept(String path) {
                        dismissDialog();
                        ToastUtils.showShort(getString(R.string.save_success));
                        startActivity(HomeActivity.class);
                    }
                });
            }
        }, 1000);
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        switch (seekBar.getId()) {
            case R.id.sbAlpha:
                mDataBinding.watermarkImg.setTextAlpha(progress);
                break;
            case R.id.sbSize:
                mDataBinding.watermarkImg.setTextSize(SizeUtils.sp2px(progress));
                break;
            default:
                break;
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
}